package com.yaorange.web.controller.certificate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.yaorange.certificate.vo.NjtCountVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yaorange.common.annotation.Log;
import com.yaorange.common.core.controller.BaseController;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.enums.BusinessType;
import com.yaorange.certificate.domain.NjtCertificateCount;
import com.yaorange.certificate.service.INjtCertificateCountService;
import com.yaorange.common.utils.poi.ExcelUtil;
import com.yaorange.common.core.page.TableDataInfo;

/**
 * 合格证统计数据Controller
 *
 * @author coach tam
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/certificate/certificate_count")
public class NjtCertificateCountController extends BaseController
{
    @Autowired
    private INjtCertificateCountService njtCertificateCountService;
    /**
     * 获取合格证开具详情数据表中所有年份
     *
     * @return 合格证开具详情数据表中所有年份
     */
    @GetMapping(value = "/yearList")
    public AjaxResult yearList() {
        List<String> list = njtCertificateCountService.getYearList();
        return AjaxResult.success(list);
    }
    /**
     * 查询合格证统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtCertificateCount njtCertificateCount)
    {
        startPage();
        List<NjtCertificateCount> list = njtCertificateCountService.selectNjtCertificateCountList(njtCertificateCount);
        return getDataTable(list);
    }

    /**
     * 导出合格证统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:export')")
    @Log(title = "合格证统计数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtCertificateCount njtCertificateCount)
    {
        List<NjtCertificateCount> list = njtCertificateCountService.selectNjtCertificateCountList(njtCertificateCount);
        ExcelUtil<NjtCertificateCount> util = new ExcelUtil<NjtCertificateCount>(NjtCertificateCount.class);
        util.exportExcel(response, list, "合格证统计数据数据");
    }

    /**
     * 获取合格证统计数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(njtCertificateCountService.selectNjtCertificateCountById(id));
    }

    /**
     * 新增合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:add')")
    @Log(title = "合格证统计数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtCertificateCount njtCertificateCount)
    {
        return toAjax(njtCertificateCountService.insertNjtCertificateCount(njtCertificateCount));
    }

    /**
     * 修改合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:edit')")
    @Log(title = "合格证统计数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtCertificateCount njtCertificateCount)
    {
        return toAjax(njtCertificateCountService.updateNjtCertificateCount(njtCertificateCount));
    }

    /**
     * 删除合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('certificate:certificate_count:remove')")
    @Log(title = "合格证统计数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(njtCertificateCountService.deleteNjtCertificateCountByIds(ids));
    }


//    getPrintCountList?unitId=10&year=2024
    @GetMapping(value = "/getPrintCountList")
    public AjaxResult getPrintCountList(Integer unitId, String year) {
        List<NjtCountVo> list = njtCertificateCountService.getPrintCountList(unitId, year);

        List<String> mouths = list.stream().map(NjtCountVo::getTitle).collect(Collectors.toList());
        List<Double> counts = list.stream().map(NjtCountVo::getCount).collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("months",mouths);
        result.put("counts",counts);
        return AjaxResult.success(result);
    }

    //streetStatisticsByArea?yearStr=2024-01-01&unitId=10
    @GetMapping("/streetStatisticsByArea")
    public AjaxResult streetStatisticsByArea(String yearStr, Long unitId) {
        List<Map<String, Object>> list = njtCertificateCountService.streetStatisticsByArea(yearStr, unitId);
        return AjaxResult.success(list);
    }



}
